﻿// Behavior Created by Liferose
#region Usings
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Xml.Linq;

using Bots.Grind;
using Buddy.Coroutines;
using CommonBehaviors.Actions;
using Honorbuddy.QuestBehaviorCore;
using Styx;
using Styx.Common;
using Styx.CommonBot;
using Styx.CommonBot.POI;
using Styx.Pathing;
using Styx.CommonBot.Profiles;
using Styx.CommonBot.Routines;
using Styx.Helpers;
using Styx.TreeSharp;
using Styx.WoWInternals;
using Styx.WoWInternals.World;
using Styx.WoWInternals.WoWObjects;

using Action = Styx.TreeSharp.Action;
#endregion


namespace Honorbuddy.Quest_Behaviors.LifeProject.SpecificQuests.Kalimdor.Desolace
{
    [CustomBehaviorFileName(@"LifeProject\14346-CleansingOurCrevasse")]
    public class CleansingOurCrevasse : QuestBehaviorBase
    {
        #region Constructor and Argument Processing
        public CleansingOurCrevasse(Dictionary<string, string> args)
            : base(args)
        {
            QBCLog.BehaviorLoggingContext = this;
            
            try
            {
                QuestId = 14346;
                QuestRequirementComplete = QuestCompleteRequirement.NotComplete;
                QuestRequirementInLog = QuestInLogRequirement.InLog;
                Location_KarnumsGlade = new WoWPoint(-978.6782, 1653.473, 67.70858);
                Location_KarnumsGladeBird = new WoWPoint(-978.0101, 1645.086, 65.95087);
            }

            catch (Exception except)
            {
                // Maintenance problems occur for a number of reasons.  The primary two are...
                // * Changes were made to the behavior, and boundary conditions weren't properly tested.
                // * The Honorbuddy core was changed, and the behavior wasn't adjusted for the new changes.
                // In any case, we pinpoint the source of the problem area here, and hopefully it can be quickly
                // resolved.
                QBCLog.Exception(except);
                IsAttributeProblem = true;
            }
        }

        protected override void EvaluateUsage_DeprecatedAttributes(XElement xElement)
        {
        }

        protected override void EvaluateUsage_SemanticCoherency(XElement xElement)
        {
        }

        public int QuestId { get; private set; }
        public WoWPoint Location_KarnumsGlade { get; private set; }
        public WoWPoint Location_KarnumsGladeBird { get; private set; }
        public QuestCompleteRequirement QuestRequirementComplete { get; private set; }
        public QuestInLogRequirement QuestRequirementInLog { get; private set; }
        #endregion


        #region Private and Convenience variables
        public LocalPlayer Me { get { return (StyxWoW.Me); } }
        #endregion

		private bool ThrowConcoction()
		{
			return Lua.GetReturnVal<bool>("if GetPetActionCooldown(1) == 0 then CastPetAction(1) return 1 else return nil end", 0);
		}

        public WoWUnit Grimfeather
        {
            get
            {
                return ObjectManager.GetObjectsOfType<WoWUnit>()
                    .Where(u => u.Entry == 36227 && !u.IsDead && u.Distance < 100)
                    .OrderBy(u => u.Distance).FirstOrDefault();
            }
        }

        public WoWUnit SlitherbladeSiren
        {
            get
            {
                return ObjectManager.GetObjectsOfType<WoWUnit>()
                    .Where(u => u.Entry == 35606 && !u.IsDead && u.Distance < 100)
                    .OrderBy(u => u.Distance).FirstOrDefault();
            }
        }

        public WoWUnit SlitherbladeInvader
        {
            get
            {
                return ObjectManager.GetObjectsOfType<WoWUnit>()
                    .Where(u => u.Entry == 35605 && !u.IsDead && u.Distance < 100)
                    .OrderBy(u => u.Distance).FirstOrDefault();
            }
        }

        public WoWUnit SeaGiant
        {
            get
            {
                return ObjectManager.GetObjectsOfType<WoWUnit>()
                    .Where(u => u.Entry == 90 && !u.IsDead && u.Distance < 100)
                    .OrderBy(u => u.Distance).FirstOrDefault();
            }
        }

        public override void OnStart()
        {
            var isBehaviorShouldRun = OnStart_QuestBehaviorCore(string.Empty);

            if (isBehaviorShouldRun)
            {
                LevelBot.BehaviorFlags &=
                    ~(BehaviorFlags.Combat | BehaviorFlags.Vendor);
            }
        }

        protected override Composite CreateBehavior_CombatOnly()
        {
            return new PrioritySelector(
                new Decorator(context => Query.IsInVehicle(),
                    new ActionAlwaysSucceed())
                );
        }

        protected override Composite CreateMainBehavior()
        {
            return new PrioritySelector(
                new Decorator (context => Me.IsQuestComplete(QuestId),
                    new Sequence(
                        new Action(context => { Lua.DoString("CastPetAction(3)"); }),
                        new Action(context => { BehaviorDone("Finished"); })                     
                        )
                    ),
                new Decorator(context => !Query.IsInVehicle() && !Navigator.AtLocation(Location_KarnumsGladeBird),
                    new Sequence(
                        new UtilityBehaviorPS.MoveTo(context => Location_KarnumsGlade, context => "Karnum's Glade", context => MovementBy),
                        // This isn't pretty but there are nav issues with this fountain
                        new UtilityBehaviorPS.MoveTo(context => Location_KarnumsGladeBird, context => "Karnum's Glade Bird", context => MovementByType.ClickToMoveOnly)                        
                        )
                    ),
                new Decorator(context => Grimfeather != null && !Me.HasAura(68557),
                    new Action(context => { Grimfeather.Interact(); })
                    ),
                new Decorator(context => !SpellManager.GlobalCooldown,
                    new PrioritySelector(
                        new Decorator(context => SeaGiant != null && !Me.IsQuestObjectiveComplete(QuestId, 3),
                            new Sequence(
                                new Action(context => { SeaGiant.Target(); }),
								new Action(context => ThrowConcoction() ? RunStatus.Success : RunStatus.Failure),
                                new Action(context => { SpellManager.ClickRemoteLocation(SeaGiant.Location); }),
                                new WaitContinue(TimeSpan.FromMilliseconds(2000), context => false, new ActionAlwaysSucceed())
                                )
                            ),
                        new Decorator(context => SlitherbladeSiren != null && !Me.IsQuestObjectiveComplete(QuestId, 2),
                            new Sequence(
                                new Action(context => { SlitherbladeSiren.Target(); }),
								new Action(context => ThrowConcoction() ? RunStatus.Success : RunStatus.Failure),
                                new Action(context => { SpellManager.ClickRemoteLocation(SlitherbladeSiren.Location); }),
                                new WaitContinue(TimeSpan.FromMilliseconds(2000), context => false, new ActionAlwaysSucceed())
                                )
                            ),
                        new Decorator(context => SlitherbladeInvader != null && !Me.IsQuestObjectiveComplete(QuestId, 4),
                            new Sequence(
                                new Action(context => { SlitherbladeInvader.Target(); }),
								new Action(context => ThrowConcoction() ? RunStatus.Success : RunStatus.Failure),
                                new Action(context => { SpellManager.ClickRemoteLocation(SlitherbladeInvader.Location); }),
                                new WaitContinue(TimeSpan.FromMilliseconds(2000), context => false, new ActionAlwaysSucceed())
                                )
                            )
                        )
                    ));
        }
    }
}
